import random
import time

import allure
from selenium.webdriver.common.by import By

from src.main.page.base_page import base_page
from .add_member_page import Add_member_page
from ..utils import Log_ou


class Contact_page(base_page):

    @allure.step('前往添加部门页面')
    def goto_add_department_page(self,department_name,department_choice=None):
        down_display_button_locate = (By.XPATH,"//*[@class='member_colLeft_top_addBtnWrap js_create_dropdown']")
        self.find_ele_is_presence(down_display_button_locate).click()

        go_to_add_department_button_locate = (By.XPATH,"//*[@class='js_create_party']")
        self.find_ele_is_presence(go_to_add_department_button_locate).click()

        #中文模式：部门名称输入框
        chinese_department_name_input_locate=(By.XPATH,"//*[preceding-sibling::*[text()='部门名称']]")

        #英文模式，部门名称输入框
        english_department_name_input_locate=(By.XPATH,"//*[preceding-sibling::*[text()='英文名称']]")

        cn_department_name_input = self.find_ele_is_presence(chinese_department_name_input_locate)

        if department_name != None:
            cn_department_name_input.send_keys(department_name)

        #选择部门下拉框
        down_department_choice_button_locate = (By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Dropdown js_toggle_party_list']")
        self.find_ele_is_presence(down_department_choice_button_locate).click()

        #选择山猪集团
        choose_big_department_locate = (By.XPATH,"//*[@class='qui_dropdownMenu ww_dropdownMenu member_colLeft js_party_list_container']//a[text()='山猪集团']")
        #选择下面的小部门:获取部门列表，然后投骰子取一个值便可
        random_choose_small_department_locate = (By.XPATH,"//*[@class='qui_dropdownMenu ww_dropdownMenu member_colLeft js_party_list_container']//*[@class='jstree-children']/li")
        eles = self.find_eles_is_presence(random_choose_small_department_locate)
        if department_choice != None:
            if department_choice == -1:
                ele = self.find_ele_is_presence(choose_big_department_locate)
            else:
                ele = eles[department_choice]
        else:
            ele = random.choice(eles)
        ele.click()

        #点击创建按钮
        save_button_locate = (By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(save_button_locate).click()

        ### 点击创建按钮后，捕获异常弹窗信息:部门名称不能超过 64 个字符 ,请输入部门名称,请选择所属部门
        try:
            time.sleep(1)
            tips=self.driver.find_element(By.XPATH,"//*[@class='ww_tip error ww_tip_Warning']")
            # expected_msg_tips_locate = (By.XPATH,"//*[@class='ww_tip error ww_tip_Warning']")
            # tips = self.find_ele_is_presence(expected_msg_tips_locate)
            print(tips.text)

            Log_ou.debug(f'保存部门后的提示信息:{tips.text}')
            return tips.text
        except:
            ##点击创建按钮后，成功添加部门的捕获信息
            time.sleep(1)
            success_tips = self.driver.find_element(By.XPATH, "//*[@class='ww_tip success']")
            # success_create_department_locate = (By.XPATH, "//*[@class='ww_tip success']")
            # success_tips = self.find_ele_is_presence(success_create_department_locate)
            print(f'添加成功后，提示信息：{success_tips.text}')
            return success_tips.text





        #点击创建后，因为选中层级已存在同名部门：弹出报错信息：该部门已存在 和上面的事一样的，只是这个点击完提交后，会退出浮窗，
        # 回到页面而上面的不会
        # expected_msg_department_has_exited_locate=(By.XPATH,"//*[@class='ww_tip error ww_tip_Warning']")

    @allure.step('删除刚刚添加的子部门')
    def delete_department_by_name(self,department_name):
        'jstree-anchor jstree-hovered'
        to_find_department_locate = (By.XPATH,"//*[@class='member_colLeft_bottom']//*[@class='jstree-children']/li[@class='jstree-node js_editable jstree-closed']/i")
        department_item_a = self.find_eles_is_presence(to_find_department_locate)
        try:
            last_one_locate = (By.XPATH,"//*[@class='member_colLeft_bottom']//*[@class='jstree-children']/li[@class='jstree-node js_editable jstree-closed jstree-last']/i")
            time.sleep(1)
            ele = self.driver.find_element(By.XPATH,"//*[@class='member_colLeft_bottom']//*[@class='jstree-children']/li[@class='jstree-node js_editable jstree-closed jstree-last']/i")
            department_item_a.append(ele)
        except:
            pass
        for i in department_item_a:
            i.click()


        all_find_department_locate = (By.XPATH,f"//*[@class='member_colLeft_bottom']//*[@class='jstree-children']/li//*[text()='{department_name}']")
        department_item_A = self.find_eles_is_presence(all_find_department_locate)
        List_tips=[]
        for i in department_item_A:
            self.driver.execute_script("arguments[0].className ='jstree-anchor jstree-hovered';", i)
            # 下拉框：选择删除
            down_choic_department_locate = (By.XPATH,
                                            f"//*[@class='member_colLeft_bottom']//*[@class='jstree-children']/li//*[text()='{department_name}']/span")
            self.find_ele_is_presence(down_choic_department_locate).click()

            # 下拉框中选择删除按钮：
            choose_delete_btn_locate = (
            By.XPATH, "//*[@class='vakata-context jstree-contextmenu jstree-default-contextmenu']/li[7]")
            self.find_ele_is_presence(choose_delete_btn_locate).click()

            # 点击确认
            confirm_btn_locate = (By.XPATH, "//*[@class='qui_btn ww_btn ww_btn_Blue']")
            self.find_ele_is_presence(confirm_btn_locate).click()
            time.sleep(1)
            # 删除成功后的提示消息捕获
            success_del_tips_locate = (By.XPATH, "//*[@class='ww_tip success']")
            success_tips = self.find_ele_is_presence(success_del_tips_locate)
            List_tips.append(success_tips.text)
            print(f'成功删除部门，提示语：{success_tips.text}')
        return List_tips











    @allure.step('前往添加成员页面')
    def goto_add_member_page(self):
        time.sleep(2)
        Log_ou.info('前往添加成员页面')
        _add_member_locate=(By.XPATH,"//*[@class='ww_operationBar']//*[@class='qui_btn ww_btn js_add_member']")

        self.find_ele_is_presence(_add_member_locate).click()

        return Add_member_page(self.driver)

    @allure.step('获取所有的成员列表-phone')
    def get_member_list_phone(self):
        Log_ou.info('获取成员列表')
        time.sleep(5)
        _get_member_list_locate_phone = (By.XPATH, "//*[@id='member_list']//tr/td[5]/span")

        # 返回成员列表
        span_eles = self.find_eles_is_presence(_get_member_list_locate_phone)


        return span_eles

    @allure.step('获取所有的成员列表-名称')
    def get_member_list(self):
        Log_ou.info('获取成员列表')
        time.sleep(5)
        _get_member_list_locate_name=(By.XPATH,"//*[@id='member_list']//tr/td[2]/span")
        #返回成员列表
        span_eles = self.find_eles_is_presence(_get_member_list_locate_name)

        return span_eles

    @allure.step('删除单个成员')
    def delete_member_single(self,index):
        #通过勾选指定位置的成员选择框，来进行删除
        Log_ou.info('删除单个成员')
        _input_list_locate = (By.XPATH,"//*[@id='member_list']//tr/td[1]/input")

        input_list = self.find_eles_is_presence(_input_list_locate)

        input_list[index].click()

        _delete_btn_locate =(By.XPATH,"//*[@class='qui_btn ww_btn js_delete']")
        self.find_ele_is_presence(_delete_btn_locate).click()
        _confirm_btn_locate=(By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(_confirm_btn_locate).click()
        success_create_department_locate = (By.XPATH, "//*[@class='ww_tip success']")
        time.sleep(1)
        success_tips = self.find_ele_is_presence(success_create_department_locate)
        return success_tips.text

    @allure.step('批量删除成员')
    def delete_member(self,index_list):
        #通过勾选指定位置的成员选择框，来进行删除
        Log_ou.info('批量删除成员')
        _input_list_locate = (By.XPATH,"//*[@id='member_list']//tr/td[1]/input")

        input_list = self.find_eles_is_presence(_input_list_locate)
        for index in index_list:
            input_list[index].click()

        _delete_btn_locate =(By.XPATH,"//*[@class='qui_btn ww_btn js_delete']")
        self.find_ele_is_presence(_delete_btn_locate).click()
        _confirm_btn_locate=(By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(_confirm_btn_locate).click()

    @allure.step('添加子部门')
    #点击部门栏的三个点，添加子部门
    def click_deparkment_btn_add_department(self):
        Log_ou.info('添加子部门')
        department_name = '冼喆之家-' + self.get_faker_name()
        _deparkment_btn_locate=(By.XPATH,"//*[@class='jstree-anchor jstree-clicked']/span")
        self.find_ele_is_presence(_deparkment_btn_locate).click()

        _add_department_btn_locate=(By.XPATH,"//*[@class='vakata-context jstree-contextmenu jstree-default-contextmenu']/li[1]")
        self.find_ele_is_presence(_add_department_btn_locate).click()

        _input_department_name_locate=(By.XPATH,"//*[@class='qui_inputText ww_inputText']")
        self.find_ele_is_presence(_input_department_name_locate).send_keys(department_name)

        _save_btn_locate=(By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(_save_btn_locate).click()
        success_create_department_locate = (By.XPATH, "//*[@class='ww_tip success']")
        success_tips = self.find_ele_is_presence(success_create_department_locate)

        return department_name,success_tips.text

    @allure.step('往新添加的子部门里添加成员')
    #添加完部门后，往该部门里添加成员
    def add_member_after_add_department(self):
        Log_ou.info('往新添加的子部门里添加成员')
        time.sleep(5)
        _add_member_btn_locate=(By.XPATH,"//*[@class='qui_btn ww_btn js_add_member']")
        # self.find_ele_is_clickable(_add_member_btn_locate).click()
        self.find_ele_is_presence(_add_member_btn_locate).click()
        return Add_member_page(self.driver)

    @allure.step('遍历每个初级部门，修改其类')
    def run_round_list_de(self):
        run_round_locate=(By.XPATH,"//*[@class='member_colLeft_bottom']//*[@class='jstree-children']/li/a")
        eles = self.find_eles_is_presence(run_round_locate)

        for i in eles:
            self.driver.execute_script("arguments[0].className ='jstree-anchor jstree-hovered';", i)


    @allure.step('点击下拉框，点击新增子部门')
    def goto_add_son_department(self):
        span_locate = (By.XPATH, "//*[@class='member_colLeft_bottom']//*[@class='jstree-children']/li/a/span")

        eles = self.find_eles_is_presence(span_locate)

        #点击下拉框
        ele = random.choice(eles)
        ele.click()
        #选择新增按钮
        _add_department_btn_locate = (
        By.XPATH, "//*[@class='vakata-context jstree-contextmenu jstree-default-contextmenu']/li[1]")
        self.find_ele_is_presence(_add_department_btn_locate).click()

        _input_department_name_locate = (By.XPATH, "//*[@class='qui_inputText ww_inputText']")
        department_name = '冼喆之家-' + self.get_faker_name()
        self.find_ele_is_presence(_input_department_name_locate).send_keys(department_name)

        _save_btn_locate = (By.XPATH, "//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(_save_btn_locate).click()

    @allure.step('点击下拉框，点击修改部门名称')
    def goto_update_department(self,department_name=None):
        span_locate = (By.XPATH, "//*[@class='member_colLeft_bottom']//*[@class='jstree-children']/li/a/span")

        eles = self.find_eles_is_presence(span_locate)

        # 点击下拉框
        ele = random.choice(eles)
        ele.click()

        # 选择修改按钮
        _add_department_btn_locate = (
            By.XPATH, "//*[@class='vakata-context jstree-contextmenu jstree-default-contextmenu']/li[3]")
        self.find_ele_is_presence(_add_department_btn_locate).click()

        #改名输入框
        update_name_input_locate = (By.XPATH,"//*[@class='qui_inputText ww_inputText js_rename_input']")
        u_name_input = self.find_ele_is_presence(update_name_input_locate)

        if department_name != None:
            u_name_input.clear()
            u_name_input.send_keys(department_name)

        save_btn_locate = (By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(save_btn_locate).click()

        tips_locate = (By.XPATH, "//*[@id='js_tips']")
        fail_tips = self.find_ele_is_presence(tips_locate)
        print(f'提示信息：{fail_tips.text}')

    @allure.step('点击下拉框，点击设置部门负责人')
    def goto_set_de_manager(self):
        span_locate = (By.XPATH, "//*[@class='member_colLeft_bottom']//*[@class='jstree-children']/li/a")

        eles = self.find_eles_is_presence(span_locate)

        # 选择一个部门
        ele = random.choice(eles)
        ele.click()
        _add_member_btn_locate = (By.XPATH, "//*[@class='qui_btn ww_btn js_add_member']")

        #先添加成员：
        try:

            add_member_page = self.add_member_after_add_department()
            time.sleep(2)
            name, p, success_text = add_member_page.add_member()

        except:
            eles = self.find_eles_is_presence(_add_member_btn_locate)
            eles[1].click()
            add_member_page = Add_member_page(self.driver)




        name, p, success_text = add_member_page.add_member()

        #下拉框
        down_locate = (By.XPATH,"//*[@class='jstree-anchor jstree-clicked']/span")
        self.find_ele_is_presence(down_locate).click()


        time.sleep(5)

        # 选择修改按钮
        _add_department_btn_locate = (
            By.XPATH, "//*[@class='vakata-context jstree-contextmenu jstree-default-contextmenu']/li[5]")
        self.find_ele_is_presence(_add_department_btn_locate).click()


        #选择负责人
        list_people_locate = (By.XPATH,"//*[@class='qui_dialog ww_dialog qui_dialog ww_dialog ww_dialog_NoWidthLimit multiselect_dialog hidden_checkbox']")
        people_list = self.find_eles_is_presence(list_people_locate)
        eles_chose = people_list[-1].find_elements(By.XPATH,"//li")
        Eles_chose = people_list[-1].find_elements(By.XPATH, "//li/div")
        for i in eles_chose:

            self.driver.execute_script("arguments[0].removeAttribute('aria-selected');", i)
            # self.driver.execute_script("arguments[0].click();", i)

        for i in Eles_chose:
            # class ="jstree-wholerow jstree-wholerow-hovered"
            self.driver.execute_script("arguments[0].removeAttribute('unselectable');", i)
            self.driver.execute_script("arguments[0].className='jstree-wholerow jstree-wholerow-hovered';", i)
            # self.driver.execute_script("arguments[0].click();", i)


        choosen_one = random.choice(eles_chose)
        self.driver.execute_script("arguments[0].click();", choosen_one)
        # js = 'document.getElementById("nice").removeAttribute("title")'

        ca = choosen_one.find_element(By.XPATH,"//a")
        name = ca.text

        #确认
        save_btn_locate = (By.XPATH,"//*[@id='footer_submit_btn']")
        self.find_ele_is_presence(save_btn_locate).click()
        check_locate = (By.XPATH,f"//*[@class='js_list ui-sortable']/tr/td[@title={name}]/span")
        num = len(self.find_eles_is_presence(check_locate))
        return num























    @allure.step('添加成员后删除成员')
    #添加完成员后，删除成员
    def delete_member_in_somedepartment(self):
        Log_ou.info('添加成员后删除成员')
        _click_member_btn_locate=(By.XPATH,"//*[@class='js_checkbox_container member_colRight_memberTable_td member_colRight_memberTable_td_Checkbox']/input")
        self.find_ele_is_presence(_click_member_btn_locate).click()

        _delete_btn_locate=(By.XPATH,"//*[@class='qui_btn ww_btn js_delete']")
        self.find_ele_is_presence(_delete_btn_locate).click()
        _confirm_delete_btn_locate = (By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(_confirm_delete_btn_locate).click()

    @allure.step('关闭子部门')
    #删除完成员后，删除刚生成的子部门
    def delete_department(self):
        Log_ou.info('关闭子部门')
        _department_more_choie_btn_locate=(By.XPATH,"//*[@class='jstree-anchor jstree-clicked']/span")
        self.find_ele_is_presence(_department_more_choie_btn_locate).click()
        _delete_btn_locate=(By.XPATH,"//*[@class='vakata-context jstree-contextmenu jstree-default-contextmenu']//*[text()='删除']")
        self.find_ele_is_presence(_delete_btn_locate).click()
        _confirm_delete_btn_locate=(By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(_confirm_delete_btn_locate).click()
        success_create_department_locate = (By.XPATH, "//*[@class='ww_tip success']")
        time.sleep(1)
        success_tips = self.find_ele_is_presence(success_create_department_locate)
        return success_tips.text



    #根据输入部门名字来添加成员
    @allure.step('根据输入部门名字来添加成员')
    def point_to_department_by_department_name(self,department_name):
        Log_ou.info(f'选中指定的部门:{department_name}')
        _department_add_btn_locate=(By.XPATH,f"//*[@class='jstree-children']//a[text()=\'{department_name}\']")
        self.find_ele_is_presence(_department_add_btn_locate).click()

    @allure.step('选择指定部门')
    #根据输入的位置index来指定部门，并且往该部门添加成员
    def point_to_department_by_index(self,index):
        Log_ou.info(f'选择第{index}个部门')
        #在xpath里index选列表是从1开始的
        _department_add_btn_locate=(By.XPATH,f"//*[@class='jstree-children']/li[{index}]/a")
        self.find_ele_is_presence(_department_add_btn_locate).click()

    @allure.step('文件导入添加成员')
    #一个已经存在成员的部门，点击文件导入
    def add_a_lot_member_when_exist(self):
        _add_a_lot_btn_locate=(By.XPATH,"//*[@class='ww_btnWithMenu ww_btnWithMenu_Open_Up js_btnWithMenu js_import_other_wrap']/a")
        self.find_ele_is_presence(_add_a_lot_btn_locate).click()

        _file_input_btn = (By.XPATH,"//*[@class='ww_btnWithMenu ww_btnWithMenu_Open_Up js_btnWithMenu js_import_other_wrap ww_btnWithMenu_Open']//a[@class='qui_dropdownMenu_itemLink ww_dropdownMenu_itemLink js_import_member']")

        self.find_ele_is_presence(_file_input_btn).click()
        _input_file_input=(By.XPATH,"//*[@class='qui_btn ww_btn ww_fileInputWrap']")
        Log_ou.warning(self.send_input_file_path())

        self.find_ele_is_presence(_input_file_input).send_keys(self.send_input_file_path())

        _save_btn_locate=(By.XPATH,"//*[@id='submit_csv']")
        time.sleep(2)
        self.find_ele_is_presence(_save_btn_locate).click()
        _goto_see_locate = (By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Large ww_btn_Blue import_succReturnBtn']")
        self.find_ele_is_presence(_goto_see_locate).click()

    @allure.step('前往标签页面')
    def goto_tag(self,tag_name=None):
        site_locate = (By.XPATH,"//*[@class='qui_btn ww_btn']")
        self.find_ele_is_presence(site_locate).click()

        add_locate = (By.XPATH,"//*[@class='member_colLeft_top_addBtnWrap']")
        self.find_ele_is_presence(add_locate).click()
        tag_name_input_locate = (By.XPATH,"//*[@class='qui_inputText ww_inputText']")
        tag_name_input = self.find_ele_is_presence(tag_name_input_locate)
        if tag_name!=None:
            tag_name_input.send_keys(tag_name)

        #选人下拉框
        down_p_locate = (By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Dropdown js_toggle_share_range']")
        self.find_ele_is_presence(down_p_locate).click()
        #选择管理员：0号位是全部，1号位是指定，2号位是自己,如果选择自己则下面的权限就不用选了
        choose_p_locate = (By.XPATH,"//*[@class='qui_dropdownMenu ww_dropdownMenu js_share_range_container']//li")
        self.find_eles_is_presence(choose_p_locate)[0].click()

        #权限下拉框:0是经使用，1是可编辑
        down_r_locate = (By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Dropdown js_toggle_share_perm']")
        self.find_ele_is_presence(down_r_locate).click()

        #选择权限
        ch_r_locate = (By.XPATH,"//*[@class='qui_dropdownMenu ww_dropdownMenu member_tag_dialog_perm_select']//li")
        self.find_eles_is_presence(ch_r_locate)[1].click()

        #确认按钮
        save_btn_locate = (By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(save_btn_locate).click()

        #捕获信息
        msg_locate = (By.XPATH,"//*[@id='js_tips']")
        tips = self.find_ele_is_presence(msg_locate)
        print(tips.text)
        return tips.text


    @allure.step('添加标签')
    #点击添加标签
    def tag(self):
        _btn_option_locate = (By.XPATH,"//*[@class='member_colLeft_top_addBtnWrap js_create_dropdown']")
        self.find_ele_is_presence(_btn_option_locate).click()
        _btn_add_tag_locate = (By.XPATH,"//*[@class='js_create_tag']")
        self.find_ele_is_presence(_btn_add_tag_locate).click()

        _input_tag_name_locate = (By.XPATH,"//*[@class='qui_inputText ww_inputText']")
        self.find_ele_is_presence(_input_tag_name_locate).send_keys('123')

        _drop_list_use_power_locate = (By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Dropdown js_toggle_share_range']")
        self.find_ele_is_presence(_drop_list_use_power_locate).click()
        #选择用户权限：位置0：所有管理员  位置1：指定人  位置2：仅自己
        _choice_user_locate = (By.XPATH,"//*[@class='qui_dropdownMenu ww_dropdownMenu js_share_range_container']//a")
        self.find_eles_is_presence(_choice_user_locate)[0].click()

        _drop_write_right_locate = (By.XPATH,"//*[@class='ww_btnWithMenu js_btnWithMenu js_share_dialog_perm_wrap js_share_dialog_perm_elem']/a")
        self.find_ele_is_presence(_drop_write_right_locate).click()

        _choice_write_right_locate=(By.XPATH,"//*[@class='qui_dropdownMenu ww_dropdownMenu member_tag_dialog_perm_select']//a")
        #选择编写权限，位置0为仅使用，位置1为可编写
        self.find_eles_is_presence(_choice_write_right_locate)[0].click()

    @allure.step('标签页面：添加成员')
    #点击进入标签页面后添加成员,选择任意部门或员工进行添加
    def add_member(self):
        #位置0为添加成员/部门，位置1为批量导入
        _btn_add_member_locate = (By.XPATH,"//*[@class='member_colRight_cnt_operation']/a")
        self.find_eles_is_presence(_btn_add_member_locate)[0].click()
        #选择任何一个员工或不萌
        _get_li_list_locate = (By.XPATH,"//*[@class='jstree-node jstree-last jstree-open']//*[@class='jstree-children']/li")
        self.find_eles_is_presence(_get_li_list_locate)[1].click()









